import { createRouter, createWebHashHistory } from "vue-router";
import { Local } from "../utils/storage";

const routes = [
  {
    path: "/",
    name: "layout",
    component: () => import("@/views/layout/index.vue"),
    redirect: "home",
    children: [
      {
        path: "home",
        name: "home",
        component: () => import("@/views/home/index.vue"),
        meta: {
          keepAlive: true,
        },
      },
      {
        path: "found",
        name: "found",
        component: () => import("@/views/found/index.vue"),
        meta: {
          keepAlive: true,
        },
      },
      {
        path: "libaray",
        name: "libaray",
        component: () => import("@/views/libaray/index.vue"),
        meta: {
          keepAlive: true,
        },
      },
      {
        path: "login",
        name: "login",
        component: () => import("@/views/login/index.vue"),
        meta: {
          keepAlive: false,
        },
      },
      {
        path: "willList",
        name: "willList",
        component: () => import("@/components/detail/willList.vue"),
      },
      {
        path: "userLike",
        name: "userLike",
        component: () => import("@/components/detail/userLike.vue"),
        meta: {
          keepAlive: true,
        },
      },
      {
        path: "daymusic",
        name: "daymusic",
        component: () => import("@/components/foryou/dayMusic.vue"),
        meta: {
          keepAlive: true,
        },
      },
      {
        path: "setUp",
        name: "setUp",
        component: () => import("@/components/common/setUp.vue"),
        meta: {
          keepAlive: true,
        },
      },
      {
        path: "/searchHome/:keyword",
        name: "searchHome",
        component: () => import("@/components/search/searchHome.vue"),
        meta: {
          keepAlive: false,
        },
      },
      {
        path: "/searchAlbumList/:keyword",
        name: "searchAlbumList",
        component: () => import("@/components/searchList/searchAlbumList.vue"),
        meta: {
          keepAlive: false,
        },
      },
      {
        path: "/searchVideoList/:keyword",
        name: "searchVideoList",
        component: () => import("@/components/searchList/searchVideoList.vue"),
        meta: {
          keepAlive: false,
        },
      },
      {
        path: "/searchPlayList/:keyword",
        name: "searchPlayList",
        component: () => import("@/components/searchList/searchPlayList.vue"),
        meta: {
          keepAlive: false,
        },
      },
      {
        path: "/searchSingerList/:keyword",
        name: "searchSingerList",
        component: () => import("@/components/searchList/searchSingerList.vue"),
        meta: {
          keepAlive: false,
        },
      },
      {
        path: "/searchSongList/:keyword",
        name: "searchSongList",
        component: () => import("@/components/searchList/searchSongList.vue"),
        meta: {
          keepAlive: false,
        },
      },
    ],
  },
  {
    path: "/loading",
    name: "loading",
    component: () => import("@/views/layout/loading.vue"),
  },
  {
    path: "/songListDetail",
    name: "songListDetail",
    component: () => import("@/components/detail/songListDetail.vue"),
  },
  {
    path: "/albumDetail",
    name: "albumDetail",
    component: () => import("@/components/detail/albumDetail.vue"),
  },
  {
    path: "/singerDetail",
    name: "singerDetail",
    component: () => import("@/components/detail/singer/index.vue"),
  },

  {
    path: "/videoDetail",
    name: "videoDetail",
    component: () => import("@/components/video/index.vue"),
  },
  {
    path: "/videos",
    name: "videos",
    component: () => import("@/components/video/videos.vue"),
  },
];

export const router = createRouter({
  // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
  history: createWebHashHistory(),
  routes, // `routes: routes` 的缩写
});

router.beforeEach((to, from, next) => {
  if (to.path == "/libaray" && !Local.get("token")) {
    router.push("/login");
  } else if (to.path == "/login" && Local.get("token")) {
    router.push("/libaray");
  } else {
    next();
  }
});
